package org.tinygroup.dbcluster.sample;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {

	public static void main(String[] args) throws Exception {

		Class.forName("org.tinygroup.dbcluster.jdbc.DbclusterDriver");
		Connection connection = DriverManager
				.getConnection("jdbc:dbcluster://dynamicdriver");
		// Class.forName("oracle.jdbc.OracleDriver");
		// Connection connection = DriverManager
		// .getConnection(
		// "jdbc:oracle:thin:@10.20.25.249:1521:orcl",
		// "C##BIZ_0", "12345678");
		DatabaseMetaData metaData = connection.getMetaData();
		// ResultSet schemas = metaData.getSchemas();
		// ResultSet schemas = metaData.getCatalogs();
		ResultSet schemas = metaData.getTables(null, "C##BIZ_1", "%",
				new String[] { "TABLE" });
		int num = 0;
		while (schemas.next()) {
			for (int i = 0; i < schemas.getMetaData().getColumnCount(); i++) {
				System.out.println(schemas.getObject(i + 1));
			}
			num++;
		}
		System.out.println("---------" + num + "-----------");
		Statement statement = connection.createStatement();
		long startTime = System.currentTimeMillis();
		// statement.execute("use bizall");
//		boolean result = statement
//				.execute("ALTER TABLE T_BRANCH  DROP PRIMARY KEY");
		ResultSet resultSet = statement
				.executeQuery("select * from ( select row_.*, rownum db_rownum from ( select * from C##TINYDBCLUSTER1.t_org order by org_id) row_ where rownum <=20 ) where db_rownum >=10");
		System.out.println("执行时间：" + (System.currentTimeMillis() - startTime));
		startTime = System.currentTimeMillis();
		for (int j = 0; j < 100; j++) {
			if (resultSet.next()) {
				for (int i = 0; i < resultSet.getMetaData().getColumnCount(); i++) {
					System.out.println(resultSet.getMetaData().getColumnName(i+1));
					System.out.println(resultSet.getObject(i + 1));
				}
				System.out.println("-------------");
			}
		}
		System.out.println("遍历时间：" + (System.currentTimeMillis() - startTime));
		// insertData(connection);
		// deleteData(connection);
		if (connection != null) {
			connection.close();
		}

	}

	private static void insertData(Connection connection) throws SQLException {
		long startTime = System.currentTimeMillis();
		System.out.println("-----insert starting-------");
		try {
			connection.setAutoCommit(false);
			PreparedStatement preparedStatement = connection
					.prepareStatement("insert into t_order(order_id,user_id,status) values(?,?,?)");
			for (int i = 0; i < 10000; i++) {
				preparedStatement.setInt(1, i + 1);
				preparedStatement.setInt(2, i + 1);
				preparedStatement.setString(3, "insert");
				preparedStatement.addBatch();
				if (i % 10000 == 0) {
					preparedStatement.executeBatch();
					System.out.println("-----executeBatch-------");
				}
			}
			preparedStatement.executeBatch();
			connection.commit();
		} catch (SQLException e) {
			connection.rollback();
		}
		System.out.println("-----insert starting-------"
				+ (System.currentTimeMillis() - startTime));
	}

	private static void deleteData(Connection connection) throws SQLException {
		long startTime = System.currentTimeMillis();
		System.out.println("-----deleteData starting-------");
		try {
			connection.setAutoCommit(false);
			// PreparedStatement
			// preparedStatement=connection.prepareStatement("truncate table aaa");
			PreparedStatement preparedStatement = connection
					.prepareStatement("delete from t_order");
			preparedStatement.execute();
			connection.commit();
		} catch (Exception e) {
			connection.rollback();
		}
		System.out.println("-----deleteData starting-------"
				+ (System.currentTimeMillis() - startTime));
	}

}
